package utils;

import java.util.Enumeration;
import java.util.Observable;

import nanoxml.XMLElement;

import commands.Commands;
import enums.PlayerColor;

/**
 * Represents a player in a game
 */
public class Player extends Observable implements ITransmittable, Comparable<Player>{

	private static final String XML_ATT_NAME = "name";
	private static final String XML_ATT_COLOR = "color";
	private static final String XML_ATT_PLAYER_STATE = "PlayerState";

	public static final int PLAYER_STATE_READY_TO_START = 0;
	public static final int PLAYER_STATE_PLAYING = 1;
	private static final int PLAYER_STATE_VIEWER = 2;
	
	private static final PlayerColor DEFAULT_PLAYER_COLOR = PlayerColor.RED;
	
	private String playerName;
	private User theUser = null;
	private PlayerColor color = DEFAULT_PLAYER_COLOR;
	private int playerState = PLAYER_STATE_VIEWER;

	
	public Player(User theUser){
		this.theUser = theUser;
		this.playerName = theUser.getUsername();
	}
	
	public Player(User theUser, PlayerColor color){
		this(theUser);
		
		this.color = color;
	}
	
	@SuppressWarnings("unchecked")
	public Player (XMLElement message) {
		
		Enumeration<XMLElement> e = message.enumerateChildren();
		while (e.hasMoreElements()) {
			XMLElement childMessage = e.nextElement();

			if (childMessage.getName().equals(Commands.USER)) {
				theUser = new User(childMessage);
			}
		}
		
		this.playerName = message.getStringAttribute(XML_ATT_NAME);
		this.color = PlayerColor.fromString(message.getStringAttribute(XML_ATT_COLOR));
		this.playerState =  message.getIntAttribute(XML_ATT_PLAYER_STATE);
	}
	
	
	@Override
	public boolean equals(Object objOtherPlayer)
	{
		Player otherPlayer = (Player)objOtherPlayer;
		
		return getPlayerName().equals(otherPlayer.getPlayerName());
	}
	
	public String toString () {
		return playerName;
	}
	
	public XMLElement toXML(){
		XMLElement message = new XMLElement ();
		message.setName(Commands.PLAYER);
		message.setAttribute (XML_ATT_NAME, playerName);
		message.setAttribute (XML_ATT_COLOR, color);
		message.setIntAttribute(XML_ATT_PLAYER_STATE,playerState);
		
		XMLElement userElement = theUser.toXML();
		message.addChild (userElement);
		return message;
		
	}
	
	@Override
	public int compareTo(Player otherPlayer)
	{
		return getPlayerName().compareTo(otherPlayer.getPlayerName());
	}
	
	public String getPlayerName () {
		return playerName;
	}

	public User getUser () {
		return theUser;
	}

	public void setUser (User theUser) {
		this.theUser = theUser;
	}
	
	public void setColor (PlayerColor color) {
		this.color = color;
	}
	
	public PlayerColor getColor () {
		return color;
	}
	
	public void readyToStart (){
		playerState = PLAYER_STATE_READY_TO_START;
	}
	
	public void startGame (){
		playerState = PLAYER_STATE_PLAYING;
	}
	
	public int getPlayerState() {
		return playerState;
	}
	
	public void setViewer()
	{
		playerState = PLAYER_STATE_VIEWER;
	}
	
	public boolean isViewer()
	{
		return (playerState == PLAYER_STATE_VIEWER);
	}
}